using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.UI;
using SubversionReports.Entities;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class TagChangeLog : UserControl
    {
        private Repository repository;

        /// <summary>
        /// Gets the repository.
        /// </summary>
        /// <value>The repository.</value>
        protected Repository Repository
        {
            get
            {
                if (repository == null)
                    repository = SvnWebDav.GetRepositoryLog(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword);
                return repository;
            }
        }

        /// <summary>
        /// Sets the tag.
        /// </summary>
        /// <value>The tag.</value>
        public Tag Tag
        {
            set { BindTagRevisions(value); }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e) { }

        /// <summary>
        /// Binds the tag revisions.
        /// </summary>
        /// <param name="tag">The tag.</param>
        private void BindTagRevisions(Tag tag)
        {
            if (tag != null)
            {
                TagLabel.Text = string.Format("{0} ({1}-{2})", tag.Name, tag.StartRevision, tag.EndRevision);
                TagDescriptionLabel.Text = string.Format("{0}, by <em>{1}</em>, on {2}", tag.Comment, tag.Author, tag.Date.ToShortDateString());

                List<Revision> revisionsInTag = Repository.FindAll(delegate(Revision revision)
                {
                    return revision.Id >= tag.StartRevision
                        && revision.Id <= tag.EndRevision
                        && revision.Paths.Find(delegate(Path path) { return path.RevisionPath.StartsWith(tag.Path); }) != null;
                });
                if (revisionsInTag != null && revisionsInTag.Count > 0)
                {
                    revisionsInTag.Sort(delegate(Revision r1, Revision r2) { return r2.Id.CompareTo(r1.Id); });
                    RevisionRepeater.DataSource = revisionsInTag;
                    RevisionRepeater.DataBind();
                    RevisionRepeater.Visible = true;
                    EmptyDataLabel.Visible = false;
                }
                else
                {
                    RevisionRepeater.Visible = false;
                    EmptyDataLabel.Visible = true;
                }
            }
        }

        protected static string ParseCommentForBugId(string comment)
        {
            return Regex.Replace(comment, ".*\nBug #([0-9]+)\n", new MatchEvaluator(GetBugLink));
        }

        static string GetBugLink(Match m)
        {
            string bugId = m.ToString().Substring(m.ToString().LastIndexOf('#') + 1).Trim();
            return m.ToString().Replace(bugId, string.Format("<a href=\"http://bugzilla.group.game.net/show_bug.cgi?id={0}\">{0}</a>", bugId));
        }

    }
}